package com.mocoop.plugin.ajax;



import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;


public class HttpUtil {

	private final String contentType = "application/json";
	private final String accept = "text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
	private final String agent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)";
	private DefaultHttpClient httpClient;
	private HttpContext localContext;
	private HttpResponse response;

	private String ret;
	private String authorization = null;


	public HttpUtil(int timeOut) {
		httpClient = new DefaultHttpClient();
		   /*连接超时*/
        HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), timeOut);
        /*请求超时*/
        HttpConnectionParams.setSoTimeout(httpClient.getParams(), timeOut);
		localContext = new BasicHttpContext();
	}

	public void setAuthorization(String authorization) {
		this.authorization = authorization;
	}

	public String postPage(String url, String data) throws ClientProtocolException, IOException {
		HttpPost httpPost;
		

		ret = null;
		data = data == null ? "" : data;
		httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,
				CookiePolicy.RFC_2109);
			httpPost = new HttpPost(url);
			response = null;
			StringEntity tmp = null;
			httpPost.setHeader("User-Agent", agent);
			httpPost.setHeader("Accept", accept);
			httpPost.setHeader("Content-Type", contentType);
		
			if (authorization != null) {
				httpPost.setHeader("Authorization", authorization);
			}
			tmp = new StringEntity(data, "UTF-8");
			httpPost.setEntity(tmp);
		
			response = httpClient.execute(httpPost, localContext);

			if ((response != null) && (response.getEntity() != null)) {
				ret = EntityUtils.toString(response.getEntity(),"UTF-8");
			}

		return ret;

	}

	public String getPage(String url) throws ClientProtocolException, IOException {
		HttpGet httpGet;
		ret = null;
		

		httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,
				CookiePolicy.RFC_2109);
			httpGet = new HttpGet(url);
			response = null;

			httpGet.setHeader("User-Agent", agent);
			httpGet.setHeader("Accept", accept);
			httpGet.setHeader("Content-Type", contentType);
			if (authorization != null) {
				httpGet.setHeader("Authorization", authorization);
			}
			
			response = httpClient.execute(httpGet, localContext);

			if ((response != null) && (response.getEntity() != null)) {
				ret = EntityUtils.toString(response.getEntity());
			}

		return ret;
	}

	
}
